﻿<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>

<script type="text/javascript">

    function addOption(text, value) {
        var oSelCode = document.getElementById("selectuser");
        var oOption = document.createElement("option");
        oOption.setAttribute("value", value);
        oOption.onclick = delOption;
        var oText = document.createTextNode(text);
        oOption.appendChild(oText);
        oSelCode.appendChild(oOption);
    }

    function delOption() {
        var oSelCode = document.getElementById("selectuser");
        var oOption = oSelCode.options[oSelCode.selectedIndex];
        oSelCode.removeChild(oOption);
        var treeObj = $.fn.zTree.getZTreeObj("treeDemo");
        var node = treeObj.getNodeByParam("name", oOption.text, null);
        if (node != null) {
            node.checked = false;
            treeObj.refresh();
        }
    }

    function delAllOption() {
        var oSelCode = document.getElementById("selectuser");
        var i;
        for (i = oSelCode.options.length - 1; i >= 0; i--) {
            var oOption = oSelCode.options[i];
            oSelCode.removeChild(oOption);
        }
    }

    function submit() {
        var creater_id = $("#creater_id").val();
        var groupname = $("#groupname").val();
        var groupmembers = $("#groupmembers").val();
        if ("" == groupname) {
            alert("请输入小组名称！");
            return false;
        }
        if ("" == groupmembers) {
            alert("至少有一个成员");
            return false;
        }

        $.ajax({
            url: "/Group/DoGroupAdd",
            type: "POST",
            data: { "creater_id": creater_id, "groupname": groupname, "groupmembers": groupmembers },
            success: function(data) {
                alert(data);
            }
        })

    }

    function onUsertreeCheck_GroupAdd(event, treeId, treeNode) {
        //alert(treeNode.id + ", " + treeNode.name + "," + treeNode.checked);
        var treeObj = $.fn.zTree.getZTreeObj("treeDemo");
        var nodes = treeObj.getCheckedNodes(true);
        var temp_name = "";
        var temp_id = "";
        delAllOption(); //删除所有节点
        for (i = 0; i < nodes.length; i++) {
            temp_id = temp_id + nodes[i].id + ",";
            temp_name = temp_name + nodes[i].name + ",";
            addOption(nodes[i].name, nodes[i].id);
        }
        $("#groupmembers").attr("value", temp_id);
    };
</script>

<div id="groupadd">
    组名：<input type="text" id="groupname" /><br />
    <div id="usertree" class="zTreeDemoBackground left" style="padding-top: 5px; width: 200px;
        float: left">
        <ul id="treeDemo" class="ztree">
        </ul>
    </div>
    <div id="" style="float: left; width: 200px; margin-left: 50px;">
        <br />
        <select id="selectuser" name="selectuser" multiple="multiple" style="width: 198px;
            height: 200px">
            <option value="red" onclick="delOption()">红色</option>
            <option value="green" onclick="delOption()">绿色</option>
            <option value="blue" onclick="delOption()">蓝色</option>
        </select><br />
    </div>
    <div id="hide">
        <%=Html.Hidden("groupmembers") %>
        <%=Html.Hidden("creater_id", ViewData["creater_id"])%>
    </div>
    <br />
    <div>
        <input type="button" value="确定" onclick="return submit();" /></div>
</div>
